using System;
using System.Runtime.CompilerServices;
	
namespace Mono4Delphi
{
	public delegate void TDataSetNotifyEvent (IntPtr sender);
	public delegate void TFieldNotifyEvent (IntPtr sender);
	
	public interface ITField: ITComponent
	{
		bool AsBoolean {get; set;}
		int AsInteger {get; set;}
		long AsLargeInt {get; set;}
		string AsString {get; set;}
		double AsFloat {get; set;}
		bool IsNull {get;}
		void Clear();
		event TFieldNotifyEvent OnChange;
	}
	
	public interface ITDataset: ITComponent
	{
		void Open();
		void Close();		
		void Next();
		bool Eof{get; }
		ITField FieldByName(string aFieldName);
		event TDataSetNotifyEvent AfterInsert;
		event TDataSetNotifyEvent BeforePost;
		event TDataSetNotifyEvent OnCalcFields;
	}
	
	
	class MainClass
	{
		public static void Main (string[] args)
		{
			Console.WriteLine ("Hello World!");
		}
		
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
	    extern public static int getFieldAsInteger(IntPtr aObj);
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
	    extern public static void setFieldAsInteger(IntPtr aObj, int aValue);
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
	    extern public static IntPtr FieldByName(IntPtr aObj, string aFieldName);
		
		public void InitPlugin(ITForm f)
		{
			ITDataset cds1 = (ITDataset)f.FindComponent("ClientDataSet1");
			cds1.OnCalcFields += HandleCds1OnCalcFields;

			colSum = FieldByName(cds1.GetHInstance(), "COLSUM");
			col2 = FieldByName(cds1.GetHInstance(), "COL2");
			col3 = FieldByName(cds1.GetHInstance(), "COL3");
		}
		
		IntPtr colSum;
		IntPtr col2;
		IntPtr col3;
		
		void HandleCds1OnCalcFields (IntPtr sender)
		{			
			setFieldAsInteger(colSum, getFieldAsInteger(col2)+getFieldAsInteger(col3));
		}
	}
}

